var player,a,b;
require("../util.js");
require("../component/components.js");
require("../component/basicSlide.js");
// require("../component/purchaseHeader.js");
// require("../component/footer.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#burstingActivity",
    data:{
        // pageNumber:1,
        // burstALL:{},
        // burstALLList:{},
        // burstALLList1:[],
        // burstALLList2:[],
        uid:"",
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        totalTime:"",//剩余时间
        // endSecond:"",
        // startsecond:"",
        lNumber:"",
        number:0,
        num:"",
        type:"0",
        loadFlag:false,
        luckyBurst:{},
        typeId:"",
        burstList:{},
        sortDate:"",
        isScreen:"",
        tip:"",
        orderNumber:"",
        user:{},
        isNav:2,
        isVideo:true,
        isflase1:false,
        release:{},
        coverImgSrc:"",
        currentTime:"",
        end:"",
        leftTime:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        if(this.searchObject&&this.searchObject.loginUid){
            this.uid=this.searchObject&&this.searchObject.loginUid;
        }else{
            this.uid=userInfo.id;
        }
        if(this.searchObject&&this.searchObject.type){
            this.type=this.searchObject&&this.searchObject.type;
            this.getLuckyBurst();
            // this.releaseConfig();
            // if(this.type==2){
            //     this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
            //     this.luckyBurstDetail();
            //     this.tip=1;
            //     setInterval(this.hideTip, 3000);
            // }else if(this.type==3){
            //     this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
            //     this.luckyBurstDetail();
            // }else{
            //     this.getLuckyBurst();
            // }
        }else{
            this.getLuckyBurst();
        }
        console.log(this.uid)
        //this.getLuckyBurstALLList();
    },
    methods: {
        //后退
        getBack: function () {
            console.log(this.type)
            if(this.type==1||this.type==2){
                location.href="/purchase";
            }else{
                history.go(-1);
            }
        },
        bgimg: function (url) {
            url = url ? url : "";
            return {
                "background-image": 'url("' + url + '"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取爆款活动类型
        getLuckyBurst:function(){
            var that=this;
            this.ajax({url:"/purchase/getLuckyBurst",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.luckyBurst=b.json.data;
                    that.luckyBurst.types.forEach(function(val){
                        if(val.sortDate==0){
                            that.typeId=val.typeId;
                            // that.currentTime=getNowFormatDate()+" "+val.currentTime;
                            // // console.log(val.endDate.split("").length);
                            // that.end=getNowFormatDate()+" "+val.endDate1;
                            // console.log(that.currentTime)
                            // console.log(that.end)
                            // clearInterval(timer);
                            //countTime();
                            clearInterval(a);
                            that.compareDate(val);
                        }
                    });
                    if(!that.typeId){
                        that.typeId=that.luckyBurst.types[0].typeId;
                        that.sortDate=that.luckyBurst.types[0].sortDate;
                    }
                    //that.typeId=that.luckyBurst.types[0].typeId;
                    that.getLuckyBurstList();

                }else{
                    vm.$refs.prompt.toast("获取类型失败！");
                }
            });
        },
        //乐购爆款活动列表
        getLuckyBurstList: function (item) {
            var that = this,data;
            if(item&&item.typeId){
                that.burstList={};
                that.typeId=item.typeId;
                that.sortDate=item.sortDate;
                clearInterval(a);
                that.compareDate(item);
            }
            data={
                typeId:that.typeId
            };
            if(this.uid){
                data.loginUid=this.uid
            }
            console.log("22222222222")
            that.user={};
            console.log(that.user)
            this.ajax({
                url: "/purchase/getLuckyBurstList",data:data,dataType: "json",method: "post"}, function (b) {
                if (b.json.code == 0 && b.json.data) {
                    that.burstList=b.json.data;
                    if(that.burstList.u){
                        that.type=1;
                        that.user=that.burstList.u;
                    }
                    console.log("----------")
                    console.log(that.user)
                } else {
                    vm.$refs.prompt.toast("获取爆款列表失败！")
                }
            });
        },
        //乐购-购买爆款优惠券
        buyLuckyBurst: function () {
            var that = this,data;
            data={
                loginUid:that.uid,
                burstCouponId:that.burstList.burstCouponId
            };
            console.log(data);
            this.ajax({
                url: "/purchase/buyLuckyBurst",data:data,dataType: "json",method: "post"}, function (b) {
                if (b.json.code == 0 && b.json.data) {
                    //that.luckyBurstDetail();
                    that.getNext(2,b.json.data.orderNumber);
                } else {
                    if(b.json.msg=="乐币不够"){
                        that.getIsScreen(2);
                    }else{
                        that.getIsScreen();
                        vm.$refs.prompt.toast(b.json.msg||"购买失败！");
                    }

                }
            });
        },
        //爆款优惠-详情
        luckyBurstDetail:function(){
            var that=this,data;
            data={
                loginUid:this.uid,
                orderNumber:that.orderNumber
            };
            this.ajax({url:"/purchase/luckyBurstDetail",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.burstList=b.json.data;
                    if(that.burstList.state==1){  //成功
                        that.getNext(5);
                    }else if(that.burstList.state==0){ //失败
                        that.getNext(7);
                    }
                    that.user=that.burstList.u;
                    that.typeId=that.burstList.userId;
                    that.sortDate=that.burstList.sortDate;
                    if(that.sortDate==0){
                        that.compareDate(that.burstList);
                    }
                    console.log(that.number)
                    that.lNumber=that.burstList.advs.length;
                    that.coverImgSrc=that.burstList.advs[that.number]&&that.burstList.advs[that.number].coverImgSrc;
                    that.luckyBurstAdvId=that.burstList.advs[that.number]&&that.burstList.advs[that.number].luckyBurstAdvId;
                    console.log(that.luckyBurstAdvId)
                    if(that.lNumber>0){
                        getPlayer(that.burstList.advs[that.number]);

                    }
                    Number(that.number++);
                    if(that.number<=that.lNumber){
                        that.title=that.burstList.advs[that.number]&&that.burstList.advs[that.number].title;
                        that.coverImgSrc=that.burstList.advs[that.number]&&that.burstList.advs[that.number].coverImgSrc;
                    }
                    // that.burstList.list=that.burstList.list;
                    // that.getLuckyBurstList();
                }else{
                    vm.$refs.prompt.toast("获取详情失败！");
                }
            });
        },
        hideTip:function(){
            console.log("111111111111");
            this.tip=0;
        },
        //获取配置文件
        releaseConfig:function(){
            var that=this,data;
            data={
                loginUid:this.uid
            };
            console.log(data)
            this.ajax({url:"/release/releaseConfig",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.release=b.json.data;
                    console.log(that.release)
                }else{
                    vm.$refs.prompt.toast("获取视频信息失败！")
                }
            });
        },
        //视频播放
        getVideoPlay:function(type){
            if(type==1){
                this.isVideo=!this.isVideo;
                this.isflase1=false;
                // this.isNav=1;
                player.play();
                this.getCanvas();
                // if(this.number>this.lNumber){
                //     this.isflase1=false;
                //     this.isNav=3;
                // }else{
                //     // this.title=this.burstList.advs[this.number].title;
                //     // this.coverImgSrc=this.burstList.advs[this.number].coverImgSrc;
                //     this.getCanvas();
                // }

            }else if(type==2){
                this.isNav=1;
                this.isVideo=true;
            }else if(type==3){
                this.isNav=2;
                player.pause();
            }
        },
        getData:function(data){
            if(data<10){
                return "0"+data+":00";
            }else{
                return data+":00";
            }
        },
        compareDate:function(item,typeId){
            var that=this;
            console.log(item)
            var dataList=item.currentTime.split(":");
            var hour,minute,second;
            hour=parseInt(item.endDate)-parseInt(dataList[0]);
            if(dataList[1]>0||dataList[2]>0){
                hour--;
                minute=60-parseInt(dataList[1]);
                if(dataList[2]>0){
                    minute--;
                    second=60-parseInt(dataList[2]);
                }
            }
            console.log(hour+"时"+minute+"分"+second+"秒");
            that.hours=hour;
            that.minutes=minute+(hour*60);
            that.seconds=second;
            if(that.hours>=0&&that.minutes>=0&&that.seconds>0){
                a=setInterval(this.leftTimer,1000);
            }else{
                that.hours="0";
                that.minutes="0";
                that.seconds="0";
            }
            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            // that.hours=Number(that.hours);
            that.minutes=Number(that.minutes);
            that.seconds=Number(that.seconds);
            if(that.minutes==0&&that.seconds==0){
                clearInterval(a);
            }else{
                that.seconds--;
                if(that.seconds == 0){
                    that.minutes--;
                    that.seconds = 59;
                }
                if(that.minutes < 0){
                    // that.hours--;
                    that.minutes = 59;
                }
                // if (that.hours <= 9) that.hours = '0' + that.hours;
                if (that.minutes <= 9) that.minutes = '0' + that.minutes;
                if (that.seconds <= 9) that.seconds = '0' + that.seconds;
            }
        },
        //下个视频
        getCanvas:function(){
            var that=this;
            b=setInterval(function(){
                var totalTime=player.getDuration();
                var whereYouAt = player.getCurrentTime();
                that.totalTime=Math.floor(totalTime)-Math.floor(whereYouAt);
                console.log(vm.totalTime)
                if(that.totalTime==0){
                    clearInterval(b);
                    that.getLuckyBurstByAdv();
                    // player.ended(function(){
                    //     console.log("ddddd")
                    // });
                }else if(that.totalTime<=40){
                    that.num=1;
                }
            },1000);
        },
        //爆款优惠-看视频获取能量
        getLuckyBurstByAdv:function(){
            var that=this,data;
            data={
                loginUid:this.uid,
                orderNumber:that.orderNumber,
                luckyBurstAdvId:that.luckyBurstAdvId
            };
            console.log(data)
            this.ajax({url:"/purchase/getLuckyBurstByAdv",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    // that.number++;
                    that.user=b.json.data.u;
                    console.log(that.number);
                    if(that.number>that.lNumber){
                        console.log("22222222222222222222222222222222222")
                        that.isflase1=false;
                        that.isNav=3;
                    }
                    if(that.burstList.advs[that.number]){
                        player.dispose();
                        $(".burstingInfo-video").append("<div id='my-video'></div>");
                        console.log(that.number)
                        that.luckyBurstAdvId=that.burstList.advs[that.number].luckyBurstAdvId;
                        that.isVideo=!that.isVideo;
                        that.isflase1=true;
                        that.isNav=2;
                        that.num="";
                        getPlayer(that.burstList.advs[that.number]);
                        player.pause();
                        Number(that.number++);
                    }else{
                        console.log("11111111111111111111")
                        that.isNav=3;
                    }
                }else{
                    vm.$refs.prompt.toast("获取能量失败！");
                }
            });
        },
        //乐购-爆款优惠-好友点赞获取能量
        getLuckyBurstByFriend:function(){
            var that=this,data;
            if(that.sortDate<0){
                vm.$refs.prompt.toast("活动还未开始,请稍后！");
                return false;
            }else if(that.sortDate>0){
                vm.$refs.prompt.toast("活动已结束！");
                return false;
            }
            data={
                loginUid:this.uid,
                orderNumber:that.orderNumber
            };
            this.ajax({url:"/purchase/getLuckyBurstByFriend",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.tip=2;
                    setInterval(that.hideTip, 3000);
                    that.user=b.json.data.u;
                    that.burstList.totalNum=b.json.data.totalNum;
                    that.burstList.userCount=b.json.data.userCount;
                    that.luckyBurstDetail();
                }else{
                    this.tip=0;
                    vm.$refs.prompt.toast(b.json.msg||"点赞失败！");
                }
            });
        },
        getIsScreen:function(num) {
            this.isScreen=num;
        },
        getNext:function(type,orderNumber){
            if(type==2){
                location.href="/purchase/burstingActivity?type="+type+"&orderNumber="+orderNumber;
            }else if(type==3){
                if(this.user&&this.user.orderNumber){
                    location.href="/purchase/burstingInfo?type="+type+"&orderNumber="+this.user.orderNumber;
                }else if(this.burstList.sortDate<0&&this.burstList.u==null){
                    vm.$refs.prompt.toast("未参与活动！");
                }else if(this.burstList.sortDate==0&&this.burstList.u==null){
                    vm.$refs.prompt.toast("支付乐币,参与活动！");
                }
            }else if(type==4){
                location.href="/purchase/goodFriend?orderNumber="+this.orderNumber;
            }else if(type==5){
                console.log(this.orderNumber)
                location.href="/purchase/activityEnd?type=1&orderNumber="+this.orderNumber;
            }else if(type==7){
                location.href="/purchase/activityEnd?type=2&orderNumber="+this.orderNumber;
            }else if(type==6){
                location.href="/purchase/activityExplain?type=3";
            }else if(type==8){
                location.href="/friend/choiceList?number=8&postId="+this.orderNumber;
            }else if(type==9){
                vm.$refs.prompt.toast("请前往APP充值！");
            }
        }
    }
    ,watch:{
        // num:function(){
        //     this.$nextTick(function(){
        //         console.log($(".secondKill-snatchCent .swiper-slide").length);
        //         var swiper = new Swiper('.swiper-container', {
        //             slidesPerView: "auto",
        //             spaceBetween: 5,
        //             loop : true,
        //             autoplay:true,
        //             observer:true,//修改swiper自己或子元素时，自动初始化swiper
        //             observeParents:true//修改swiper的父元素时，自动初始化swiper
        //         });
        //     });
        // }
    }
});
function getPlayer(imgSrc){
    player = new Aliplayer({
        id: 'my-video',
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc.imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,
        "securityToken": vm.release.securityToken,
        "accessKeySecret": vm.release.accessKeySecret,
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "controlBarVisibility": "click",
        "showBarTime": 0,
        "useH5Prism": true
    },function(player){
        console.log('播放器创建好了。');
        $("#my-video video").attr("poster",vm.coverImgSrc);
    });
}
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//倒计时
var timer=null;
function countDown(){
    timer=setInterval(function(){
        var day=0,
            hour=0,
            minute=0,
            second=0;//时间默认值
        if(vm.leftTime>0){
            // day = Math.floor(vm.leftTime/1000/60/60/24);
            // hour = Math.floor(vm.leftTime/1000/60/60%24);
            minute = Math.floor(vm.leftTime/1000/60%60);
            second = Math.floor(vm.leftTime/1000%60);
        }
        vm.minutes=checkTime(minute);
        vm.seconds=checkTime(second);
        // console.log(day+"天:"+hour+"小时："+minute+"分钟："+second+"秒");
        console.log(vm.minutes+":"+vm.seconds);
        vm.leftTime--;
        console.log(vm.leftTime)
    },1000);
    if(vm.leftTime<=0){
        clearInterval(timer);
    }
}
function countTime() {
    //获取当前时间
    var date = new Date(vm.currentTime);
    var now = date.getTime();
    //设置截止时间
    var endDate = new Date(vm.end);
    var end = endDate.getTime();
    //时间差
    vm.leftTime = end-now;
    countDown();
}
//获取当前时间，格式YYYY-MM-DD
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var loadFlag;
window.onscroll=function(){
    //loadFlag = vm.burstALLList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            //vm.loadFlag=true;
            //vm.data.pageNumber++;
            //vm.getLuckyBurstALLList();
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}